﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CBMS.Common;
using CBMS.Interfaces;
using CBMS.Business;
using CBMS.Models;
using CBMS.Resources;
using CBMS.Objects;
namespace CBMS
{
    public partial class frmLogin : Form
    {
        #region Fields
        private string strError = string.Empty;
        public bool IsStartApplication = true;
        public event EventHandler LoginSystem;
        public static IStaffRepository staffRep { get; set; }
        public string userLogin = string.Empty;
        #endregion

        #region Init,load
        public frmLogin(Boolean start)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            txtUserName.Focus();
            txtUserName.Select();
            CommonFunction.MakeComboBoxBranch(cmbBranch, true);
            IsStartApplication = start;
        }
        #endregion

        #region Methods
        private bool CheckValidationForm()
        {
            bool result = true;
            Control txt = new Control();
            Control ctrFocus = new Control();
            string strNameText = string.Empty;
            string strAsign = "erro";
            string strErr = ErrorMessages.E0001;
            this.strError = string.Empty;

            if (txtUserName.Text.Trim() == string.Empty)
            {
                strNameText = strAsign;
                txt = (Control)txtUserName;
                this.strError += string.Format(strErr, BaseFunctions.Removetext(lblUsername.Text)) + "\r\n";
                if (result)
                {
                    ctrFocus = (Control)txtUserName;
                    result = false;
                }
            }
            if (txtPassword.Text.Trim() == string.Empty)
            {
                strNameText = strAsign;
                txt = (Control)txtPassword;
                this.strError += string.Format(strErr, BaseFunctions.Removetext(lblPassword.Text)) + "\r\n";
                if (result)
                {
                    ctrFocus = (Control)txtPassword;
                    result = false;
                }
            }
            //set focus at postition first error
            BaseFunctions.SetFocusWhenError(ctrFocus, result, strNameText);
            return result;
        }
        #endregion

        #region Events
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (!CheckValidationForm())
            {
                MessageBox.Show(this.strError, ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                staffRep = new StaffRepository();
                int? branchId = null;
                if (cmbBranch.SelectedIndex > 0)
                {
                    branchId = int.Parse(cmbBranch.SelectedValue.ToString());
                }
                if (staffRep.Login(branchId, txtUserName.Text.Trim(), txtPassword.Text.Trim()))
                {
                    this.userLogin = txtUserName.Text;
                    if (IsStartApplication)
                    {
                        this.DialogResult = DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        LoginSystem(this, EventArgs.Empty);
                    }

                }
                else
                {
                    MessageBox.Show(string.Format(ErrorMessages.I0003, "Login"), ErrorMessages.T001, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        #endregion  
    }
}
